package com.sky.controller.user;

import com.sky.annotation.AutoFill;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api("C端-地址簿")
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    @ApiOperation("地址添加")
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook){
        log.info("地址添加:{}",addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    @ApiOperation("地址显示")
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> list =addressBookService.listByUserId(userId);
        return Result.success(list);
    }
    @ApiOperation("根据id获取地址")
    @GetMapping("/{id}")
    public Result<AddressBook> listById(@PathVariable Long id){
        log.info("根据id获取地址:{}",id);
        AddressBook addressBook =addressBookService.listById(id);
        return Result.success(addressBook);
    }

    @ApiOperation("修改地址")
    @PutMapping
    public Result updateAddressBook(@RequestBody AddressBook addressBook){
        log.info("修改地址:{}",addressBook);
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }


    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result addressBookDefault(@RequestBody AddressBook addressBook){
        log.info("设为默认地址:{}",addressBook.getId());
        addressBookService.addressBookDefault(addressBook.getId());
        return Result.success();
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping()
    public Result deleteAddressBookById(@RequestParam Long id){
        log.info("删除地址:{}",id);
        addressBookService.deleteAddressBookById(id);
        return Result.success();
    }
}
